//
//  MeatCartUIComponent.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/25.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatFoldUIComponent.h"


@implementation MeatFoldUIComponent

#pragma mark - setter methods

- (void)setView:(UIView *)view
{
    [super setView:view];
    
    [view addSubview:self.pigTypeWeightLab];
    [view addSubview:self.pigTypePriceSignalLab];
    [view addSubview:self.newsText];
    [view addSubview:self.foldLabel];
    [view addSubview:self.bottomSpaceView];
}

- (void)setType:(MeatFoldUILayoutType)type{
    
    [super setType:type];
    
    self.newType = type;
    
    
    if (type == MeatFoldUILayoutType_HalfScreen) {
        
        
        [self.shopIcon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(15);
            make.top.mas_equalTo(15);
            make.width.height.mas_equalTo(80);
        }];
    }
    
    
    {
        [self.pigTypeTipLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.shopIcon.mas_right).mas_offset(13);
            make.top.mas_equalTo(15);
            make.width.mas_equalTo(48);
            make.height.mas_equalTo(20);
        }];
    }


    {
        if (type == MeatFoldUILayoutType_FullScreen) {

            [self.pigTypeNOLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.pigTypeTipLab.mas_right).mas_offset(4);
                make.top.mas_equalTo(15);
                make.right.mas_equalTo(-30);
                make.height.mas_equalTo(20);
            }];

        }else{

            [self.pigTypeNOLab mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.pigTypeTipLab.mas_right).mas_offset(4);
                make.top.mas_equalTo(15);
                make.right.mas_equalTo(-15);
                make.height.mas_equalTo(20);
            }];
        }
    }


    {
        [self.pigTypeWeightLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.shopIcon.mas_right).mas_offset(13);
            make.top.mas_equalTo(self.pigTypeTipLab.mas_bottom).mas_offset(7);
            make.width.mas_equalTo(66);
            make.height.mas_equalTo(16);
        }];
    }


    {
        [self.pigTypePriceSignalLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.pigTypeWeightLab.mas_right).mas_offset(5);
            make.top.mas_equalTo(self.pigTypeWeightLab.mas_top);
            make.width.mas_equalTo(89);
            make.height.mas_equalTo(16);
        }];

    }


    {
        [self.pigTypePriceLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.shopIcon.mas_right).mas_offset(13);
            make.top.mas_equalTo(self.pigTypeWeightLab.mas_bottom).mas_offset(34);
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(20);
        }];
    }


    {

        [self.pigTypePriceCancelLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.pigTypePriceLab.mas_right).mas_offset(5);
            make.top.mas_equalTo(self.pigTypePriceLab.mas_top);
            make.width.mas_equalTo(48);
            make.height.mas_equalTo(20);
        }];
    }


    {

        if (type == MeatFoldUILayoutType_FullScreen) {

            [self.newsText mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(47);
                make.right.mas_equalTo(-50);
                make.top.mas_equalTo(self.shopIcon.mas_bottom).mas_offset(30);
            }];

            self.newsText.preferredMaxLayoutWidth = KScreenWidth - 120;

        }else{

            [self.newsText mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(15);
                make.right.mas_equalTo(-50);
                make.top.mas_equalTo(self.shopIcon.mas_bottom).mas_offset(30);
            }];
            self.newsText.preferredMaxLayoutWidth = KScreenWidth *0.7-40;
        }

     }


    {

        [self.bottomSpaceView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.newsText.mas_bottom).mas_offset(10);
            make.left.mas_equalTo(self.newsText.mas_left);
            make.right.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(0);
        }];
    }


    {
        if (type == MeatFoldUILayoutType_FullScreen) {

            [self.foldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.newsText.mas_right).mas_offset(5);
                make.width.mas_equalTo(40);
                make.height.mas_equalTo(16);
                make.top.mas_equalTo(self.newsText.mas_bottom).offset(-18);
            }];

        }else if (type == MeatFoldUILayoutType_HalfScreen){

            [self.foldLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(self.newsText.mas_right);
                make.width.mas_equalTo(50);
                make.height.mas_equalTo(20);
                make.top.mas_equalTo(self.newsText.mas_bottom).offset(-20);
            }];
        }
    }

    if (type == MeatFoldUILayoutType_FullScreen) {

        self.checkBtn.size = CGSizeMake(48, 48);
        self.checkBtn.mj_x = 0;
        self.checkBtn.mj_y = 42;

    }else if (type == MeatFoldUILayoutType_HalfScreen){

        self.checkBtn.size = CGSizeMake(30, 30);
        self.checkBtn.mj_y = 84;
        self.checkBtn.mj_x = TJ_MeatShop_RightSideWidth - self.checkBtn.width - 20;
    }
    
    self.pigTypeTipLab.text = @"五花肉";
    self.pigTypeNOLab.text = @"HT-02";
    self.pigTypeWeightLab.text = @"重量10kg";
    self.pigTypePriceSignalLab.text = @"单价12元/500g";
    NSMutableAttributedString *aims = [@"¥172.8" createDeleteStringByTextColor:UIColorFromRGB(0x999999) font:mFont(12)];
    self.pigTypePriceCancelLab.attributedText = aims;
    self.pigTypePriceLab.text = @"¥216";
}


- (void)setModel:(MeatShopRightModel *)model{
    
    [super setModel:model];
    
    self.newsText.text = model.desc;
    // 可以在这里修改行间距，下面在计算文本高度的时候也要对应设置
    // 如果不需要修改，可以省去这一步，但注意下面获取高度的时候不要再设置行间距
    if (self.newsText.text.length > 0) {
        NSMutableAttributedString *img_text = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", model.desc]];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:3];
        [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
        [img_text addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, self.newsText.text.length)];
        
        self.newsText.attributedText = img_text;
    }
    // 获取文本内容宽度，计算展示全部文本所需高度
    CGFloat contentW = TJ_MeatShop_RightSideWidth-65 ;
    NSString *contentStr = self.newsText.text;
    
    NSMutableParagraphStyle *descStyle = [[NSMutableParagraphStyle alloc]init];
    [descStyle setLineSpacing:3];//行间距
    
    CGRect textRect = [contentStr
                       boundingRectWithSize:CGSizeMake(contentW, MAXFLOAT)
                       options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading
                       attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15.0f], NSParagraphStyleAttributeName : descStyle}
                       context:nil];
    // 这里的高度60是通过指定显示三行文字时，通过打印得到的一个临界值，根据需要自行修改
    // 超过三行文字，折叠按钮不显示
    if (textRect.size.height > 30) {
        
        self.foldLabel.hidden = NO;
        // 修改按钮的折叠打开状态
        if (model.isOpening) {
            
            self.newsText.numberOfLines = 0;
            self.foldLabel.text = @"收起";
        }else{
            
            self.newsText.numberOfLines = 1;
            self.foldLabel.text = @"展开";
        }
    }else{
        
        self.newsText.numberOfLines = 0;
        self.foldLabel.hidden = YES;
    }
    
    
    NSString *image = nil;
    if (self.newType == MeatFoldUILayoutType_HalfScreen) {
        
        if (model.isChicked) {
            self.checkBtn.userInteractionEnabled = NO;
        }else{
            self.checkBtn.userInteractionEnabled = YES;
        }

        image = model.isChicked ?@"meat_shop_cart_gray" :@"meat_shop_cart_light";
    }else{
        self.checkBtn.selected = YES;
        image = model.isChicked ?@"meat_cart_checked" :@"meat_cart_unchecked";
    }
    
    [self.checkBtn setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
}


#pragma mark - getter methods

- (UILabel *)pigTypeWeightLab {
    
    if (!_pigTypeWeightLab){
        _pigTypeWeightLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x9C9C9C) font:mFont(10) textAliment:1];
        _pigTypeWeightLab.backgroundColor = UIColorFromRGB(0xF2F4F7);
        [self addShadowAndCircleCorner:_pigTypeWeightLab.layer corner:9];
    }
    return _pigTypeWeightLab;
}


- (UILabel *)pigTypePriceSignalLab{
    
    if (!_pigTypePriceSignalLab){
        _pigTypePriceSignalLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x9C9C9C) font:mFont(10) textAliment:1];
        _pigTypePriceSignalLab.backgroundColor = UIColorFromRGB(0xF2F4F7);
        [self addShadowAndCircleCorner:_pigTypePriceSignalLab.layer corner:9];
    }
    return _pigTypePriceSignalLab;
}


- (UILabel *)newsText{
    
    if (!_newsText){
        _newsText = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x999999) font:mFont(12) textAliment:0];
    }
    return _newsText;
}


- (UILabel *)foldLabel{
    
    if (!_foldLabel) {
        _foldLabel = [UILabel makeLabelWithText:@"" textColor:mMainColor font:mFont(12) textAliment:0];
        _foldLabel.hidden = YES;
        [_foldLabel sizeToFit];
        
        kWeakSelf(self);
        _foldLabel.tapBlock = ^{
            
            if (weakself.dealFlodBlock) {
                weakself.dealFlodBlock();
            }
        };
    }
    return _foldLabel;
}


- (UIView *)bottomSpaceView {
    
    if (!_bottomSpaceView){
        _bottomSpaceView = [[UIView alloc] init];
        _bottomSpaceView.backgroundColor = UIColorFromRGB(0xF2F2F2);
    }
    return _bottomSpaceView;
}


@end
